package com.example.alipay.anysign.demo;


import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import cn.org.bjca.seal.esspdf.client.message.ChannelMessage;
import cn.org.bjca.seal.esspdf.client.utils.JsonMapper;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Test;



/***************************************************************************
 * <pre></pre>
 * @文件名称:  HttpClientAnyWriteTestDemo.java
 * @包   路   径：  cn.org.bjca.seal.esspdf.client.test
 * @版权所有：北京数字认证股份有限公司 (C) 2018
 *
 * @类描述:
 * @版本: V2.0
 * @创建人： lyr
 * @创建时间：2018-11-20 下午4:38:37
 ***************************************************************************/

public class HttpClientAnyWriteTestDemo {

	/**
	 * 组装json请求数据
	 * 手写签名门面：加密包签名
	 * @throws Exception
	 */
	@Test
	public void testAnySignEncPackage() throws Exception {
		//请求地址
		String serverUrl = "http://127.0.0.1:8092/anySignServices/pdfServer";
		//加密包(base64)
		String encDataBase64 = Base64.encodeBase64String(FileUtils.readFileToByteArray(new File("D:\\手写签名加密包.txt")));
		String headStr ="{\n" +
				"\t\"type\": \"2001\",\n" +
				"\t\"externalAnyWriteInfoList\": [\n" +
				"\t\t{\n" +
				"\t\t\t\"channel\": \"999999\",\n" +
				"\t\t\t\"encryptPackageInfoList\": [\n" +
				"\t\t\t\t{\n" +
				"\t\t\t\t\t\"encData\":\""+encDataBase64+"\"\n" +
				"\t\t\t\t}\n" +
				"\t\t\t],\n" +
				"\t\t\t\"useStorage\": false\n" +
				"\t\t}\n" +
				"\t]\n" +
				"}\n";
		byte[] dataBty = this.mergeData(headStr.getBytes());
		byte[] pdfSignBty = this.send(serverUrl, dataBty);
		if(pdfSignBty.length>10){
			FileUtils.writeByteArrayToFile(new File("D:/testAnyWriteSign.pdf"), pdfSignBty);
		} else {
			System.out.println("签名错误，状态码："+new String(pdfSignBty));
		}
	}


	/**
	 * 组装json请求数据
	 * 手写签名门面：加密包签名+外传原文
	 * @throws Exception
	 */
	@Test
	public void testAnyWriteSign() throws Exception {
		//请求地址
		String serverUrl = "http://127.0.0.1:8092/anySignServices/pdfServer";
		//待签名的pdf文件(base64)
		String pdfBase64 = Base64.encodeBase64String(FileUtils.readFileToByteArray(new File("D:\\test.ofd")));
		//加密包(base64)
		String encDataBase64 = Base64.encodeBase64String(FileUtils.readFileToByteArray(new File("D:\\手写签名加密包-OFD-SM2.txt")));
		String headStr1 ="{\"externalAnyWriteInfoList\":[{\"channel\":\"999999\",\"encryptPackageInfoList\":[{\"encData\":\""+encDataBase64+"\"}],\"useStorage\":false}],\"sealHeight\":0,\"sealWidth\":0,\"taskID\":\"0d1b7cc3-317a-46f9-aa26-fab06a7d30bf\",\"type\":\"2001\"}";
		byte[] dataBty = this.mergeData(headStr1.getBytes());
		byte[] pdfSignBty = this.send(serverUrl, dataBty);
		if(pdfSignBty.length>10){
			FileUtils.writeByteArrayToFile(new File("D:/testAnyWriteSign111.ofd"), pdfSignBty);
		} else {
			System.out.println("签名错误，状态码："+new String(pdfSignBty));
		}
	}

	/**
	 * 组装json请求数据
	 * 手写签名门面纯外传：一名一签,坐标方式
	 * @throws Exception
	 */
	@Test
	public void testAnyWriteSignByXyzrule() throws Exception {
		//请求地址
		 String serverUrl = "http://192.168.228.32:8092/anySignServices/pdfServer";
		//待签名的pdf文件(base64)
		String pdfBase64 = Base64.encodeBase64String(FileUtils.readFileToByteArray(new File("D:\\test.pdf")));
		//签名图片(base64)
		String imageBase64 =  Base64.encodeBase64String(FileUtils.readFileToByteArray(new File("D:\\test.gif")));
		String headStr ="{\n" +
				"\t\"type\": \"2001\",\n" +
				"\t\"externalAnyWriteInfoList\": [{\n" +
				"\t\t\"channel\": \"999999\",\n" +
				"\t\t\"generatePDFInfoList\": [{\n" +
				"\t\t\t\"documentExtType\": \"PDF\",\n" +
				"\t\t\t\"docContent\": \""+pdfBase64+"\"\n" +
				"\t\t}],\n" +
				"\t\t\"usignInfoList\": [{\n" +
				"\t\t\t\"imageBty\": \""+imageBase64+"\",\n" +
				"\t\t\t\"signAlg\": \"RSA\",\n" +
				"\t\t\t\"imageFMT\": \"image/png\",\n" +
				"\t\t\t\"cid\": \"21\",\n" +
				"\t\t\t\"isTSS\": \"false\",\n" +
				"\t\t\t\"signRule\": {\n" +
				"\t\t\t\t\"xyzrule\": {\n" +
				"\t\t\t\t\t\"left\": \"300\",\n" +
				"\t\t\t\t\t\"bottom\": \"400\",\n" +
				"\t\t\t\t\t\"top\": \"300\",\n" +
				"\t\t\t\t\t\"right\": \"300\",\n" +
				"\t\t\t\t\t\"unit\": \"dp\",\n" +
				"\t\t\t\t\t\"pageno\": \"1\"\n" +
				"\t\t\t\t},\n" +
				"\t\t\t\t\"ruleType\": \"1\"\n" +
				"\t\t\t},\n" +
				"\t\t\t\"imageSize\": {\n" +
				"\t\t\t\t\"height\": \"41\",\n" +
				"\t\t\t\t\"unit\": \"dp\",\n" +
				"\t\t\t\t\"width\": \"100\"\n" +
				"\t\t\t},\n" +
				"\t\t\t\"points\": {\n" +
				"\t\t\t\t\"certOID\": {\n" +
				"\t\t\t\t\t\"idtype\": \"1\",\n" +
				"\t\t\t\t\t\"idnumber\": \"150423198009210019\"\n" +
				"\t\t\t\t}\n" +
				"\t\t\t},\n" +
				"\t\t\t\"signer\": {\n" +
				"\t\t\t\t\"idtype\": \"1\",\n" +
				"\t\t\t\t\"idnumber\": \"150423198009210019\",\n" +
				"\t\t\t\t\"uname\": \"张三\"\n" +
				"\t\t\t}\n" +
				"\t\t}]\n" +
				"\t}],\n" +
				"\t\"taskID\": \"366e412e-a21c-467d-83bf-21cc2a83db93\"\n" +
				"}";
		byte[] dataBty = this.mergeData(headStr.getBytes());
		byte[] pdfSignBty = this.send(serverUrl, dataBty);
		if(pdfSignBty.length>10){
			FileUtils.writeByteArrayToFile(new File("D:/testAnyWriteSignByXyzrule.pdf"), pdfSignBty);
		} else {
			System.out.println("签名错误，状态码："+new String(pdfSignBty));
		}
	}

	/**
	 * 组装json请求数据
	 * 手写签名门面纯外传：一名一签,关键字方式
	 * @throws Exception
	 */
	@Test
	public void testAnyWriteSignByKwrule() throws Exception {
		//请求地址
		String serverUrl = "http://127.0.0.1:8092/anySignServices/pdfServer";
		//待签名的pdf文件(base64)
		String pdfBase64 = Base64.encodeBase64String(FileUtils.readFileToByteArray(new File("E:\\test.pdf")));
		//签名图片(base64)
		String imageBase64 =  Base64.encodeBase64String(FileUtils.readFileToByteArray(new File("E:\\1.gif")));
		String headStr ="{\n" +
				"\t\"type\": \"2001\",\n" +
				"\t\"externalAnyWriteInfoList\": [{\n" +
				"\t\t\"channel\": \"999999\",\n" +
				"\t\t\"generatePDFInfoList\": [{\n" +
				"\t\t\t\"documentExtType\": \"PDF\",\n" +
				"\t\t\t\"docContent\": \""+pdfBase64+"\"\n" +
				"\t\t}],\n" +
				"\t\t\"useStorage\": false,\n" +
				"\t\t\"usignInfoList\": [{\n" +
				"\t\t\t\"imageBty\": \""+imageBase64+"\",\n" +
				"\t\t\t\"signAlg\": \"RSA\",\n" +
				"\t\t\t\"isBM\": \"true\",\n" +
				"\t\t\t\"cid\": \"21\",\n" +
				"\t\t\t\"imageFMT\": \"image/png\",\n" +
				"\t\t\t\"isTSS\": \"true\",\n" +
				"\t\t\t\"imageSize\": {\n" +
				"\t\t\t\t\"height\": \"41\",\n" +
				"\t\t\t\t\"unit\": \"dp\",\n" +
				"\t\t\t\t\"width\": \"100\"\n" +
				"\t\t\t},\n" +
				"\t\t\t\"signer\": {\n" +
				"\t\t\t\t\"idtype\": \"1\",\n" +
				"\t\t\t\t\"idnumber\": \"1101230199301023002\",\n" +
				"\t\t\t\t\"uname\": \"张三\"\n" +
				"\t\t\t},\n" +
				"\t\t\t\"points\": {\n" +
				"\t\t\t\t\"certOID\": {\n" +
				"\t\t\t\t\t\"idtype\": \"1\",\n" +
				"\t\t\t\t\t\"idnumber\": \"1101230199301023002\"\n" +
				"\t\t\t\t}\n" +
				"\t\t\t},\n" +
				"\t\t\t\"signRule\": {\n" +
				"\t\t\t\t\"ruleType\": \"0\",\n" +
				"\t\t\t\t\"kwrule\": {\n" +
				"\t\t\t\t\t\"pageno\": \"1\",\n" +
				"\t\t\t\t\t\"yoffset\": \"30\",\n" +
				"\t\t\t\t\t\"kwoffset\": \"0\",\n" +
				"\t\t\t\t\t\"kwpos\": \"4\",\n" +
				"\t\t\t\t\t\"kw\": \"的\",\n" +
				"\t\t\t\t\t\"xoffset\": \"0\"\n" +
				"\t\t\t\t}\n" +
				"\t\t\t}\n" +
				"\t\t}]\n" +
				"\t}],\n" +
				"\t\"taskID\": \"5626468d-5cee-4a42-969f-39f46bf13af4\"\n" +
				"}";
		byte[] dataBty = this.mergeData(headStr.getBytes());
		byte[] pdfSignBty = this.send(serverUrl, dataBty);
		if(pdfSignBty.length>10){
			FileUtils.writeByteArrayToFile(new File("C:/Users/lyr/Desktop/KW.pdf"), pdfSignBty);
		} else {
			System.out.println("签名错误，状态码："+new String(pdfSignBty));
		}
	}


	/**
	 * 组装json请求数据
	 * 手写签名门面纯外传：一名多签,坐标方式
	 * @throws Exception
	 */
	@Test
	public void testMultiAnyWriteSignByXyzrule() throws Exception {
		//请求地址
		String serverUrl = "http://127.0.0.1:8092/anySignServices/pdfServer";
		//待签名的pdf文件(base64)
		String pdfBase64 = Base64.encodeBase64String(FileUtils.readFileToByteArray(new File("E:\\test.pdf")));
		//签名图片(base64)
		String imageBase64 =  Base64.encodeBase64String(FileUtils.readFileToByteArray(new File("E:\\1.gif")));
		String headStr ="{\n" +
				"\t\"type\": \"2001\",\n" +
				"\t\"externalAnyWriteInfoList\": [{\n" +
				"\t\t\"channel\": \"999999\",\n" +
				"\t\t\"generatePDFInfoList\": [{\n" +
				"\t\t\t\"documentExtType\": \"PDF\",\n" +
				"\t\t\t\"docContent\": \""+pdfBase64+"\"\n" +
				"\t\t}],\n" +
				"\t\t\"usignInfoList\": [{\n" +
				"\t\t\t\"imageBty\": \""+imageBase64+"\",\n" +
				"\t\t\t\"signAlg\": \"RSA\",\n" +
				"\t\t\t\"cid\": \"21\",\n" +
				"\t\t\t\"isTSS\": \"false\",\n" +
				"\t\t\t\"points\": {\n" +
				"\t\t\t\t\"certOID\": {\n" +
				"\t\t\t\t\t\"idnumber\": \"1101230199301023002\",\n" +
				"\t\t\t\t\t\"idtype\": \"1\"\n" +
				"\t\t\t\t}\n" +
				"\t\t\t},\n" +
				"\t\t\t\"signer\": {\n" +
				"\t\t\t\t\"idnumber\": \"1101230199301023002\",\n" +
				"\t\t\t\t\"idtype\": \"1\",\n" +
				"\t\t\t\t\"uname\": \"张三\"\n" +
				"\t\t\t},\n" +
				"\t\t\t\"imageFMT\": \"image/png\",\n" +
				"\t\t\t\"imageSize\": {\n" +
				"\t\t\t\t\"width\": \"100\",\n" +
				"\t\t\t\t\"height\": \"41\",\n" +
				"\t\t\t\t\"unit\": \"dp\"\n" +
				"\t\t\t},\n" +
				"\t\t\t\"signRule\": {\n" +
				"\t\t\t\t\"ruleType\": \"1\",\n" +
				"\t\t\t\t\"xyzrule\": {\n" +
				"\t\t\t\t\t\"top\": \"300\",\n" +
				"\t\t\t\t\t\"right\": \"300\",\n" +
				"\t\t\t\t\t\"bottom\": \"400\",\n" +
				"\t\t\t\t\t\"pageno\": \"1\",\n" +
				"\t\t\t\t\t\"left\": \"300\",\n" +
				"\t\t\t\t\t\"unit\": \"dp\"\n" +
				"\t\t\t\t}\n" +
				"\t\t\t}\n" +
				"\t\t},\n" +
				"\t\t{\n" +
				"\t\t\t\"imageBty\": \""+imageBase64+"\",\n" +
				"\t\t\t\"signAlg\": \"RSA\",\n" +
				"\t\t\t\"isBM\": \"true\",\n" +
				"\t\t\t\"cid\": \"22\",\n" +
				"\t\t\t\"isTSS\": \"false\",\n" +
				"\t\t\t\"points\": {\n" +
				"\t\t\t\t\"certOID\": {\n" +
				"\t\t\t\t\t\"idnumber\": \"1101230199301023002\",\n" +
				"\t\t\t\t\t\"idtype\": \"1\"\n" +
				"\t\t\t\t}\n" +
				"\t\t\t},\n" +
				"\t\t\t\"signer\": {\n" +
				"\t\t\t\t\"idnumber\": \"1101230199301023002\",\n" +
				"\t\t\t\t\"idtype\": \"1\",\n" +
				"\t\t\t\t\"uname\": \"李四\"\n" +
				"\t\t\t},\n" +
				"\t\t\t\"imageFMT\": \"image/png\",\n" +
				"\t\t\t\"imageSize\": {\n" +
				"\t\t\t\t\"width\": \"100\",\n" +
				"\t\t\t\t\"height\": \"41\",\n" +
				"\t\t\t\t\"unit\": \"dp\"\n" +
				"\t\t\t},\n" +
				"\t\t\t\"signRule\": {\n" +
				"\t\t\t\t\"ruleType\": \"1\",\n" +
				"\t\t\t\t\"xyzrule\": {\n" +
				"\t\t\t\t\t\"top\": \"200\",\n" +
				"\t\t\t\t\t\"right\": \"200\",\n" +
				"\t\t\t\t\t\"bottom\": \"100\",\n" +
				"\t\t\t\t\t\"pageno\": \"1\",\n" +
				"\t\t\t\t\t\"left\": \"100\",\n" +
				"\t\t\t\t\t\"unit\": \"dp\"\n" +
				"\t\t\t\t}\n" +
				"\t\t\t}\n" +
				"\t\t}]\n" +
				"\t}],\n" +
				"\t\"taskID\": \"92ac1fa3-b374-43b2-9c1f-d6ffe186f210\"\n" +
				"}";
		byte[] dataBty = this.mergeData(headStr.getBytes());
		byte[] pdfSignBty = this.send(serverUrl, dataBty);
		if(pdfSignBty.length>10){
			FileUtils.writeByteArrayToFile(new File("C:/Users/lyr/Desktop/Multixyz.pdf"), pdfSignBty);
		} else {
			System.out.println("签名错误，状态码："+new String(pdfSignBty));
		}
	}


	/**
	 * 组装json请求数据
	 * 手写签名门面纯外传：一名多签,关键字方式
	 * @throws Exception
	 */
	@Test
	public void testMultiAnyWriteSignByKwrule() throws Exception {
		//请求地址
		String serverUrl = "http://127.0.0.1:8092/anySignServices/pdfServer";
		//待签名的pdf文件(base64)
		String pdfBase64 = Base64.encodeBase64String(FileUtils.readFileToByteArray(new File("E:\\test.pdf")));
		//签名图片(base64)
		String imageBase64 =  Base64.encodeBase64String(FileUtils.readFileToByteArray(new File("E:\\1.gif")));
		String headStr ="{\n" +
				"\t\"type\": \"2001\",\n" +
				"\t\"externalAnyWriteInfoList\": [{\n" +
				"\t\t\"channel\": \"999999\",\n" +
				"\t\t\"generatePDFInfoList\": [{\n" +
				"\t\t\t\"documentExtType\": \"PDF\",\n" +
				"\t\t\t\"docContent\": \""+pdfBase64+"\"\n" +
				"\t\t}],\n" +
				"\t\t\"usignInfoList\": [{\n" +
				"\t\t\t\"imageBty\": \""+imageBase64+"\",\n" +
				"\t\t\t\"signAlg\": \"RSA\",\n" +
				"\t\t\t\"imageFMT\": \"image/png\",\n" +
				"\t\t\t\"points\": {\n" +
				"\t\t\t\t\"certOID\": {\n" +
				"\t\t\t\t\t\"idtype\": \"1\",\n" +
				"\t\t\t\t\t\"idnumber\": \"1101230199301023002\"\n" +
				"\t\t\t\t}\n" +
				"\t\t\t},\n" +
				"\t\t\t\"isTSS\": \"true\",\n" +
				"\t\t\t\"signRule\": {\n" +
				"\t\t\t\t\"ruleType\": \"0\",\n" +
				"\t\t\t\t\"kwrule\": {\n" +
				"\t\t\t\t\t\"pageno\": \"1\",\n" +
				"\t\t\t\t\t\"kwpos\": \"4\",\n" +
				"\t\t\t\t\t\"kw\": \"的\",\n" +
				"\t\t\t\t\t\"yoffset\": \"30\",\n" +
				"\t\t\t\t\t\"xoffset\": \"0\",\n" +
				"\t\t\t\t\t\"kwoffset\": \"0\"\n" +
				"\t\t\t\t}\n" +
				"\t\t\t},\n" +
				"\t\t\t\"cid\": \"21\",\n" +
				"\t\t\t\"imageSize\": {\n" +
				"\t\t\t\t\"width\": \"100\",\n" +
				"\t\t\t\t\"height\": \"41\",\n" +
				"\t\t\t\t\"unit\": \"dp\"\n" +
				"\t\t\t},\n" +
				"\t\t\t\"signer\": {\n" +
				"\t\t\t\t\"idtype\": \"1\",\n" +
				"\t\t\t\t\"idnumber\": \"1101230199301023002\",\n" +
				"\t\t\t\t\"uname\": \"张三\"\n" +
				"\t\t\t}\n" +
				"\t\t},\n" +
				"\t\t{\n" +
				"\t\t\t\"imageBty\": \""+imageBase64+"\",\n" +
				"\t\t\t\"signAlg\": \"RSA\",\n" +
				"\t\t\t\"imageFMT\": \"image/png\",\n" +
				"\t\t\t\"points\": {\n" +
				"\t\t\t\t\"certOID\": {\n" +
				"\t\t\t\t\t\"idtype\": \"1\",\n" +
				"\t\t\t\t\t\"idnumber\": \"220120192101204210\"\n" +
				"\t\t\t\t}\n" +
				"\t\t\t},\n" +
				"\t\t\t\"isTSS\": \"true\",\n" +
				"\t\t\t\"signRule\": {\n" +
				"\t\t\t\t\"ruleType\": \"0\",\n" +
				"\t\t\t\t\"kwrule\": {\n" +
				"\t\t\t\t\t\"pageno\": \"1\",\n" +
				"\t\t\t\t\t\"kwpos\": \"1\",\n" +
				"\t\t\t\t\t\"kw\": \"合同编号\",\n" +
				"\t\t\t\t\t\"yoffset\": \"0\",\n" +
				"\t\t\t\t\t\"xoffset\": \"0\",\n" +
				"\t\t\t\t\t\"kwoffset\": \"0\"\n" +
				"\t\t\t\t}\n" +
				"\t\t\t},\n" +
				"\t\t\t\"cid\": \"22\",\n" +
				"\t\t\t\"imageSize\": {\n" +
				"\t\t\t\t\"width\": \"100\",\n" +
				"\t\t\t\t\"height\": \"41\",\n" +
				"\t\t\t\t\"unit\": \"dp\"\n" +
				"\t\t\t},\n" +
				"\t\t\t\"signer\": {\n" +
				"\t\t\t\t\"idtype\": \"1\",\n" +
				"\t\t\t\t\"idnumber\": \"220120192101204210\",\n" +
				"\t\t\t\t\"uname\": \"王五\"\n" +
				"\t\t\t}\n" +
				"\t\t}]\n" +
				"\t}],\n" +
				"\t\"taskID\": \"62a09be2-3c74-45f0-b88c-9a8e5b6fd1da\"\n" +
				"}";
		byte[] dataBty = this.mergeData(headStr.getBytes());
		byte[] pdfSignBty = this.send(serverUrl, dataBty);
		if(pdfSignBty.length>10){
			FileUtils.writeByteArrayToFile(new File("C:/Users/lyr/Desktop/MultiKW.pdf"), pdfSignBty);
		} else {
			System.out.println("签名错误，状态码："+new String(pdfSignBty));
		}
	}



	/**
	 * 组装json请求数据
	 * 手写签名门面纯外传：一名多签,坐标+关键字方式
	 * @throws Exception
	 */
	@Test
	public void testMultiAnyWriteSignByXyzKwrule() throws Exception {
		//请求地址
		String serverUrl = "http://127.0.0.1:8092/anySignServices/pdfServer";
		//待签名的pdf文件(base64)
		String pdfBase64 = Base64.encodeBase64String(FileUtils.readFileToByteArray(new File("E:\\test\\pdf\\test.pdf")));
		//签名图片(base64)
		String imageBase64 =  Base64.encodeBase64String(FileUtils.readFileToByteArray(new File("E:\\test\\image\\test.gif")));
		String headStr ="{\n" +
				"\t\"type\": \"2001\",\n" +
				"\t\"externalAnyWriteInfoList\": [{\n" +
				"\t\t\"channel\": \"999999\",\n" +
				"\t\t\"generatePDFInfoList\": [{\n" +
				"\t\t\t\"documentExtType\": \"PDF\",\n" +
				"\t\t\t\"docContent\": \""+pdfBase64+"\"\n" +
				"\t\t}],\n" +
				"\t\t\"usignInfoList\": [{\n" +
				"\t\t\t\"imageBty\": \""+imageBase64+"\",\n" +
				"\t\t\t\"signAlg\": \"RSA\",\n" +
				"\t\t\t\"cid\": \"21\",\n" +
				"\t\t\t\"isTSS\": \"false\",\n" +
				"\t\t\t\"points\": {\n" +
				"\t\t\t\t\"certOID\": {\n" +
				"\t\t\t\t\t\"idnumber\": \"1101230199301023002\",\n" +
				"\t\t\t\t\t\"idtype\": \"1\"\n" +
				"\t\t\t\t}\n" +
				"\t\t\t},\n" +
				"\t\t\t\"signer\": {\n" +
				"\t\t\t\t\"idnumber\": \"1101230199301023002\",\n" +
				"\t\t\t\t\"idtype\": \"1\",\n" +
				"\t\t\t\t\"uname\": \"张三\"\n" +
				"\t\t\t},\n" +
				"\t\t\t\"imageFMT\": \"image/png\",\n" +
				"\t\t\t\"imageSize\": {\n" +
				"\t\t\t\t\"width\": \"100\",\n" +
				"\t\t\t\t\"height\": \"41\",\n" +
				"\t\t\t\t\"unit\": \"dp\"\n" +
				"\t\t\t},\n" +
				"\t\t\t\"signRule\": {\n" +
				"\t\t\t\t\"ruleType\": \"1\",\n" +
				"\t\t\t\t\"xyzrule\": {\n" +
				"\t\t\t\t\t\"top\": \"300\",\n" +
				"\t\t\t\t\t\"right\": \"300\",\n" +
				"\t\t\t\t\t\"bottom\": \"400\",\n" +
				"\t\t\t\t\t\"pageno\": \"1\",\n" +
				"\t\t\t\t\t\"left\": \"300\",\n" +
				"\t\t\t\t\t\"unit\": \"dp\"\n" +
				"\t\t\t\t}\n" +
				"\t\t\t}\n" +
				"\t\t},\n" +
				"\t\t{\n" +
				"\t\t\t\"imageBty\": \""+imageBase64+"\",\n" +
				"\t\t\t\"signAlg\": \"RSA\",\n" +
				"\t\t\t\"imageFMT\": \"image/png\",\n" +
				"\t\t\t\"points\": {\n" +
				"\t\t\t\t\"certOID\": {\n" +
				"\t\t\t\t\t\"idtype\": \"1\",\n" +
				"\t\t\t\t\t\"idnumber\": \"220120192101204210\"\n" +
				"\t\t\t\t}\n" +
				"\t\t\t},\n" +
				"\t\t\t\"isTSS\": \"true\",\n" +
				"\t\t\t\"signRule\": {\n" +
				"\t\t\t\t\"ruleType\": \"0\",\n" +
				"\t\t\t\t\"kwrule\": {\n" +
				"\t\t\t\t\t\"pageno\": \"1\",\n" +
				"\t\t\t\t\t\"kwpos\": \"1\",\n" +
				"\t\t\t\t\t\"kw\": \"合同编号\",\n" +
				"\t\t\t\t\t\"yoffset\": \"0\",\n" +
				"\t\t\t\t\t\"xoffset\": \"0\",\n" +
				"\t\t\t\t\t\"kwoffset\": \"0\"\n" +
				"\t\t\t\t}\n" +
				"\t\t\t},\n" +
				"\t\t\t\"cid\": \"22\",\n" +
				"\t\t\t\"imageSize\": {\n" +
				"\t\t\t\t\"width\": \"100\",\n" +
				"\t\t\t\t\"height\": \"41\",\n" +
				"\t\t\t\t\"unit\": \"dp\"\n" +
				"\t\t\t},\n" +
				"\t\t\t\"signer\": {\n" +
				"\t\t\t\t\"idtype\": \"1\",\n" +
				"\t\t\t\t\"idnumber\": \"220120192101204210\",\n" +
				"\t\t\t\t\"uname\": \"王五\"\n" +
				"\t\t\t}\n" +
				"\t\t}]\n" +
				"\t}],\n" +
				"\t\"taskID\": \"62a09be2-3c74-45f0-b88c-9a8e5b6fd1da\"\n" +
				"}";
		byte[] dataBty = this.mergeData(headStr.getBytes());
		byte[] pdfSignBty = this.send(serverUrl, dataBty);
		if(pdfSignBty.length>10){
			FileUtils.writeByteArrayToFile(new File("C:/Users/lyr/Desktop/MultiXyzKwrule.pdf"), pdfSignBty);
		} else {
			System.out.println("签名错误，状态码："+new String(pdfSignBty));
		}
	}

	@Test
	public void testAnyWriteDataSign2() throws Exception {
		//请求地址
		String serverUrl = "http://127.0.0.1:8092/anySignServices/pdfServer";
		String pathname =  "D:\\数据签名加密包.txt";
		byte[] encPackage = FileUtils.readFileToByteArray(new File(pathname));
		String headStr = "{\"type\": \"4001\"}";
		byte[] dataBty = this.mergeData(headStr.getBytes(), encPackage);
		byte[] dataSignBty = this.send(serverUrl, dataBty);
		if(dataSignBty.length>10){
			FileUtils.writeByteArrayToFile(new File("D:\\testAnyWriteDataSign.txt"), dataSignBty);
		} else {
			System.out.println("签名错误，状态码："+new String(dataSignBty));
		}
	}


	@Test
	public void testAnyWriteDataVerify() throws Exception {
		//请求地址
		String serverUrl = "http://127.0.0.1:8092/anySignServices/pdfServer";
		String pathname =  "D:\\testAnyWriteDataSign.txt";
		byte[] encPackage = FileUtils.readFileToByteArray(new File(pathname));
		String headStr = "{\"type\": \"4002\",\"contents\":\"x+uy4srUyrHKudPDuLTU07XE1K3OxA==\"}";
		byte[] dataBty = this.mergeData(headStr.getBytes(), encPackage);
		byte[] verifyBty = this.send(serverUrl, dataBty);
		if(verifyBty.length>10){
			FileUtils.writeByteArrayToFile(new File("D:\\testAnyWriteDataVerify.txt"), verifyBty);
		} else {
			System.out.println("验签错误，状态码："+new String(verifyBty));
		}
	}


	/**
	 * 组装请求消息结构
	 * @param reqBty
	 * @return
	 */
	private byte[] mergeData(byte[] reqBty) {
		//组head的长度
		ByteBuffer headLengthByteBuf = ByteBuffer.allocate(4);
		headLengthByteBuf.putInt(reqBty.length);
		byte[] headLengthBty =  headLengthByteBuf.array();
		//组head的内容
		ByteBuffer headByteBuf = ByteBuffer.allocate(reqBty.length);
		headByteBuf.put(reqBty);
		byte[] headBty = headByteBuf.array();
		//组body的长度
		ByteBuffer bodyLengthByteBuf = ByteBuffer.allocate(4);
		bodyLengthByteBuf.putInt(0);
		byte[] bodyLengthBty =bodyLengthByteBuf.array();
		//组body的内容默认为空
		byte[] bodyBty = "".getBytes(Charset.forName("UTF-8"));
		byte[] allBty = new byte[headLengthBty.length + headBty.length + bodyLengthBty.length + bodyBty.length];
		System.arraycopy(headLengthBty, 0, allBty, 0, headLengthBty.length);
		System.arraycopy(headBty, 0, allBty, headLengthBty.length, headBty.length);
		System.arraycopy(bodyLengthBty, 0, allBty, headLengthBty.length + headBty.length, bodyLengthBty.length);
		System.arraycopy(bodyBty, 0, allBty, headLengthBty.length + headBty.length + bodyLengthBty.length, bodyBty.length);
		return allBty;
	}

	private byte[] mergeData(byte[] reqBty, byte[] pdfBty) {

		ByteBuffer byteBuf = ByteBuffer.allocate(4);
		byteBuf.putInt(reqBty.length);
		byte[] headLengthBty = byteBuf.array();

		ByteBuffer headByteBuf = ByteBuffer.allocate(reqBty.length);
		headByteBuf.put(reqBty);
		byte[] headBty = headByteBuf.array();

		ByteBuffer byteBuf1 = ByteBuffer.allocate(4);
		byteBuf1.putInt(pdfBty.length);
		byte[] bodyLengthBty = byteBuf1.array();
		byte[] bodyBty = pdfBty;
		byte[] allBty = new byte[headLengthBty.length + headBty.length + bodyLengthBty.length + bodyBty.length];
		System.arraycopy(headLengthBty, 0, allBty, 0, headLengthBty.length);
		System.arraycopy(headBty, 0, allBty, headLengthBty.length, headBty.length);
		System.arraycopy(bodyLengthBty, 0, allBty, headLengthBty.length + headBty.length, bodyLengthBty.length);
		System.arraycopy(bodyBty, 0, allBty, headLengthBty.length + headBty.length + bodyLengthBty.length, bodyBty.length);
		return allBty;
	}


	/**
	 *发送http请求并接受解析响应信息
	 * @param serverUrl
	 * @param bty
	 * @return
	 * @throws Exception
	 */
//	private byte[] send(String serverUrl, byte[] bty) throws Exception {
//		//以下为http请求
//		OutputStream output = null;
//		InputStream is = null;
//		byte[] result = null;
//		try{
//			URL url = new URL(serverUrl);
//			HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
//			HttpURLConnection.setFollowRedirects(true);
//			httpConn.setDoOutput(true);
//			httpConn.setRequestMethod("POST");
//			httpConn.setRequestProperty("Content-Type", "text/xml");
//			httpConn.setRequestProperty("Content-Length", String.valueOf(bty.length));
//			httpConn.setConnectTimeout(5000);
//			httpConn.setReadTimeout(5000);
//			httpConn.connect();
//			output = httpConn.getOutputStream();
//			output.write(bty);
//			output.flush();
//			// 写入请求参数
//			int code = httpConn.getResponseCode();
//			// 组装响应内容
//			if (code == 200) {
//				is = httpConn.getInputStream();
//				byte[] bb = null;// head(4)+bodylength(4) = 8
//				int n = 0;
//				int headLength = 0;
//				for (int i = 0; i < 3; i++) {
//					if (i == 0) {
//						bb = new byte[4];
//						is.read(bb, 0, 4);
//						ByteBuffer byteBuf = ByteBuffer.allocate(4);
//						byteBuf.put(bb);
//						n = byteBuf.getInt(0);
//						headLength = n;
//					} else if (i == 1) {
//						bb = new byte[headLength];
//						is.read(bb, 0, headLength);
//					} else if (i == 2) {
//						bb = new byte[4];
//						is.read(bb, 0, 4);
//						ByteBuffer byteBuf = ByteBuffer.allocate(4);
//						byteBuf.put(bb);
//						n = byteBuf.getInt(0);
//					}
//				}
//				if (n > 0) {
//					byte[] bty2 = new byte[n];
//					int offset = 0;
//					while (true) {
//						int len = is.read(bty2, offset, n - offset);
//						if (len + offset == n) {
//							break;
//						}
//						offset = len + offset;
//					}
//					result = bty2;
//				}
//			} else {
//				String sTotalString = "远程服务器连接失败,错误代码:" + code;
//				System.out.println("response:" + sTotalString);
//			}
//		} finally {
//			IOUtils.closeQuietly(is);
//			IOUtils.closeQuietly(output);
//		}
//		return result;
//	}


	/**
	 * 请求服务
	 * @param serverUrl 请求地址
	 * @param bty 数据
	 * @return byte
	 * @throws Exception 异常
	 */
	private byte[] send(String serverUrl, byte[] bty) throws Exception {
		String responseHead="";
		OutputStream output = null;
		InputStream is = null;
		byte[] pdfBty = null;
		byte[] headBty = null;
		try{
			URL url = new URL(serverUrl);
			HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
			HttpURLConnection.setFollowRedirects(true);
			httpConn.setDoOutput(true);
			httpConn.setRequestMethod("POST");
			httpConn.setRequestProperty("Content-Type", "text/xml");
			httpConn.setRequestProperty("Content-Length", String.valueOf(bty.length));
			httpConn.setConnectTimeout(50000);
			httpConn.setReadTimeout(50000);
			httpConn.connect();
			output = httpConn.getOutputStream();
			output.write(bty);
			output.flush();
			// 写入请求参数
			int code = httpConn.getResponseCode();
			// 读取响应内容
			if (code == 200) {
				is = httpConn.getInputStream();
				byte[] bb = null;// head(4)+bodylength(4) = 8
				int n = 0;
				int headLength = 0;
				for (int i = 0; i < 3; i++) {
					if (i == 0) {
						bb = new byte[4];
						is.read(bb, 0, 4);
						ByteBuffer byteBuf = ByteBuffer.allocate(4);
						byteBuf.put(bb);
						n = byteBuf.getInt(0);
						headLength = n;
					} else if (i == 1) {
						bb = new byte[headLength];
						is.read(bb, 0, headLength);
						responseHead=new String(bb);
						System.out.println("responseHead："+responseHead);
					} else if (i == 2) {
						bb = new byte[4];
						is.read(bb, 0, 4);
						ByteBuffer byteBuf = ByteBuffer.allocate(4);
						byteBuf.put(bb);
						n = byteBuf.getInt(0);
					}
				}
				if (n > 0) {
					byte[] bty2 = new byte[n];
					int offset = 0;
					while (true) {
						int len = is.read(bty2, offset, n - offset);
						if (len + offset == n) {
							break;
						}
						offset = len + offset;
					}
					pdfBty = bty2;
				}
			} else {
				String sTotalString = "远程服务器连接失败,错误代码:" + code;
				System.out.println("response:" + sTotalString);
			}
		} finally {
			IOUtils.closeQuietly(is);
			IOUtils.closeQuietly(output);
		}
		return pdfBty;
	}



}
